package com.mew.chess.core.game.yxx;


import cn.hutool.core.util.StrUtil;
import com.mew.chess.core.common.handler.AbstractEnterExitHandler;
import com.mew.chess.core.common.handler.ChessEEAnnotation;
import com.mew.chess.core.common.message.request.EnterGameRequestMessage;

import com.mew.chess.core.game.lh.game.LhGameManager;
import com.mew.chess.core.game.yxx.game.YxxGameManager;
import com.mew.chess.core.game.yxx.game.YxxRoomManager;
import com.mew.chess.core.game.yxx.game.YxxStartService;
import com.mew.chess.core.game.yxx.game.common.YxxConstantValue;
import com.mew.chess.core.game.yxx.game.common.YxxLinkType;
import com.mew.chess.core.game.yxx.game.message.YxxEResponseMessage;
import com.mew.chess.core.game.yxx.game.message.YxxUserMessage;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.mew.common.rest.bo.MewSession;
import com.mew.common.rest.game.ChessEnterRoomResponse;
import com.mew.common.util.DateUtils;

import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@ChessEEAnnotation("YXX")
@Service
@Slf4j
public class YxxEEHandler extends AbstractEnterExitHandler {


    @Autowired
    private YxxStartService yxxStartService;
    @Override
    public ResponseCode doSitDown(int roomId, GameSession chessSession, MewSession session, boolean isRepeat) {

        //1 校验
        YxxRoomManager roomManager = YxxGameManager.getInstance().getController(roomId);
        if (null == roomManager){
            yxxStartService.initializeRoomManager(chessSession.getGameId(), roomId);
            roomManager = YxxGameManager.getInstance().getController(roomId);
            if (null == roomManager){
                log.error("FishRoomManager roomId {} is null", roomId);
                return ResponseCode.SysException ;
            }
        }
        if (roomManager.getOnlineUser().size() > YxxConstantValue.MAX_USER){
            log.error("LHRoomManager roomId {} room user is full", chessSession.getRoomId());
            return ResponseCode.SysException;
        }


        return ResponseCode.Success;
    }

    @Override
    public void doFinish(GameSession chessSession, ZebraCommand command, ChessEnterRoomResponse enterRoomResponse) {


        YxxRoomManager roomManager = YxxGameManager.getInstance().getController(chessSession.getRoomId());
        roomManager.registerOnlineUser(chessSession);
        YxxUserMessage userVO = roomManager.getPlayer(chessSession.getUserId());

        //2 开始游戏  发消息
        if (!roomManager.checkIsNoStart()){
            roomManager.startGame();
        }

        YxxEResponseMessage responseMessage = new YxxEResponseMessage();
        responseMessage.setUserMessage(userVO);
        responseMessage.setRoomBetArray(StrUtil.splitToInt(chessSession.getRoomBetArray(), ','));


        responseMessage.setLinkType(roomManager.getLinkType());
        responseMessage.setRemainTime(DateUtils.remainTimeMillis(roomManager.getStartTime() == 0 ? System.currentTimeMillis() : roomManager.getStartTime(), roomManager.getRuleTime()));
        responseMessage.setIno(roomManager.getIno());
        responseMessage.setHistoryVOList(roomManager.getHistoryVOList());
        responseMessage.setBigRList(roomManager.getBigRList());

        responseMessage.setBetInfoMessage(roomManager.getBetInfoMessage());
        responseMessage.setDealCardMessage(roomManager.getDealCardMessage());
        responseMessage.setPaymentMessage(roomManager.getPaymentMessage());

        if (responseMessage.getLinkType() == YxxLinkType.Payment.code()){
            responseMessage.setPaymentNumber(userVO.getPaymentNumber());
        }
        log.debug("进入游戏 {} ",responseMessage.toString());
        command.setToken(chessSession.getTk());
        command.sendMessage(responseMessage);

    }

    @Override
    public boolean doExitRoom(MewSession session, int roomId) {
        if(!session.checkChessPlaying()) {
            YxxRoomManager roomManager = YxxGameManager.getInstance().getController(roomId);
            roomManager.exitRoom(session.uid);
            return false;
        }
        return true;
    }



}
